import React,{Component} from 'react';
import {Form, Input, Switch, Button, Spin} from 'antd';
import {messages} from "share/common";
import 'styles/supplier-management/new-consumer-management/edit-frams/edit-frame.scss'
import NewConsumerServer from "../new-consumer-server";
const FormItem = Form.Item;

@Form.create()
class YgetEditFrame extends Component{

  constructor(props){
    super(props);
    this.isInit = false;
    this.isLock = false;
    this.state = {
      loading: false,
      editData: {},
    }
  }

  componentWillMount(){
    const {params} = this.props;
    this.setState({
      editData: params,
    })
  }

  // componentWillReceiveProps(nextProps){
  //   const { params } = nextProps;
  //   if(params.data){
  //     this.setState({editData: JSON.parse(JSON.stringify(params.data[params.index]))});
  //     if(params.companyCode && !this.isInit){
  //       this.props.form.resetFields();
  //       this.isInit = true;
  //     }
  //   }
  // }

  onCancelEdit = () => {
    this.isInit = false;
    this.props.close();
    this.props.form.resetFields();
  };

  onSubmitEdit = () => {
    if(!this.isLock){
      this.isLock = true;
    } else {
      return;
    }
    this.props.form.validateFieldsAndScroll((err, values) => {
      if(!err){
        const upParams = Object.assign({},{
          companyList:[
            {
              companyOID:this.state.editData.companyOID,
              companyName:values.companyName,
            }
          ],
        }, values);
        delete upParams.companyCode;
        delete upParams.companyName;
        NewConsumerServer(this).saveYgetData(upParams).then(res => {
          this.isLock = false;
          this.props.close(true);
          this.props.form.resetFields();
        });
      } else {
        this.isLock = false;
      }
    })
  }

  render(){
    const {getFieldDecorator} = this.props.form;
    const {loading, editData} = this.state;
    const formLayout = {
      labelCol: {span: 6, offset: 1},
      wrapperCol: {span: 14}
    }
    return (
      <div className='edit-frame'>
        <Spin spinning={loading}>
          <Form className='new-form'>
            <FormItem label={messages('supplier-1.key130')/*公司代码*/} {...formLayout}>
              {
                getFieldDecorator('companyCode',{
                  initialValue: editData.companyCode,
                })(<Input disabled />)
              }
            </FormItem>
            <FormItem label={messages('supplier-1.key33')/*公司名称*/} {...formLayout}>
              {
                getFieldDecorator('companyName',{
                  initialValue: editData.companyName || '',
                })(<Input disabled />)
              }
            </FormItem>
            <FormItem label={messages('common.column.status')/*状态*/} {...formLayout}>
              {
                getFieldDecorator('isOpen',{
                  initialValue: editData.enable || false,
                  valuePropName: 'checked'
                })(<Switch />)
              }
            </FormItem>
          </Form>
        </Spin>
        <br/><br/><br/><br/>
        <div className='affix-bottom'>
          <Button type="primary"  onClick={this.onSubmitEdit}>
            {messages('common.ok')/*确定*/}
          </Button>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <Button onClick={this.onCancelEdit}>
            {messages('common.cancel')/*取消*/}
          </Button>
        </div>
      </div>
    )
  }
}

YgetEditFrame.getEditData = (record, data, index) => {
  console.log(record, data, index);
  return record;
};

export default YgetEditFrame;
